require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIGuildTech = {};
HelperFunc.SetUIEnv(UIGuildTech)
local this = UIGuildTech;
 
function UIGuildTech.Init()--todo 应该小写 暂时先大写 
    local name,package = getUIName(PanelNames.UIGuildTech);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end
 
function UIGuildTech.onCreate(obj)
 
    if not this.hasInit then
        this.hasInit = true 
        this.btn_close = obj:GetChild("Btn_Close")
        this.btn_help = obj:GetChild("btn_help")
        this.btn_reset = obj:GetChild("btn_reset")
        this.btn_levelup = obj:GetChild("btn_levelup")
        this.btn_quicklevelup = obj:GetChild("btn_quicklevelup")

        this.ctrl_show_source = obj:GetController("Showsource")
        this.btn_source = obj:GetChild("btnSource")
        this.sourceUIobj = obj:GetChild("UISourceMultiple")

        this.btn_skill = obj:GetChild("btn_skill")
        this.txt_skill_level = this.btn_skill:GetChild("text_level")
        this.ctrl_skill_hero_type = this.btn_skill:GetController("HeroType")
        this.ctrl_skill_hero_type:SetSelectedIndex(0) 
        this.ctrl_skill_max = this.btn_skill:GetController("Max")
        -- this.btn_skill:GetChild("n3"):GetChild("Icon").url = "ui://SkillAtlas/" .. "100005001"
        
        this.group_txt_name = obj:GetChild("text_name")
        this.group_txt_improve = obj:GetChild("text_improve")
        local com_improve01 = obj:GetChild("com_improve01")
        this.ctrl_improve01_type = com_improve01:GetController("type")
        this.improve01_txt_herotype = com_improve01:GetChild("text_herotype")
        this.improve01_txt_title = com_improve01:GetChild("text_title")
        this.improve01_txt_numafter = com_improve01:GetChild("text_numAfter")
        this.improve01_txt_numbefore = com_improve01:GetChild("text_numBefore")
        local com_improve02 = obj:GetChild("com_improve02")
        this.ctrl_improve02_type = com_improve02:GetController("type")
        this.improve02_txt_herotype = com_improve02:GetChild("text_herotype")
        this.improve02_txt_title = com_improve02:GetChild("text_title")
        this.improve02_txt_numafter = com_improve02:GetChild("text_numAfter")
        this.improve02_txt_numbefore = com_improve02:GetChild("text_numBefore")

        this.img_coin01 = obj:GetChild("Img_coin01")
        this.txt_coin01 = obj:GetChild("Text_coin01")
        this.img_coin02 = obj:GetChild("Img_coin02")
        this.txt_coin02 = obj:GetChild("Text_coin02")

        this.img_coin03 = obj:GetChild("Img_coin03")
        this.txt_coin03 = obj:GetChild("Text_coin03")
        this.img_coin04 = obj:GetChild("Img_coin04")
        this.txt_coin04 = obj:GetChild("Text_coin04")

        local txt_title = obj:GetChild("Text_title")
        txt_title.text = "战团科技"

        this.ctrl_hero_type = obj:GetController("HeroType")
        this.ctrl_lock = obj:GetController("lock")
        this.ctrl_item_enough = obj:GetController("enough")
        this.lvUpCtrl = obj:GetController("LevelUp")
        this.bgClose = obj:GetChild("BgClose")
        this.upList = obj:GetChild("list")
        this.lvUptitle = obj:GetChild("title")
        this.upList.itemRenderer = this.UpRender

        this.tb_btn_attributes = {}
        this.cur_select_skill_index = 1
        for i=1, 6 do
            local index1 = i
            local btn_attri = obj:GetChild("btn_attribute0"..i)
            this.tb_btn_attributes[index1] = {}
            local effect_levelup = btn_attri:GetChild("effect_levelup")
            local effect_levelup_ani_obj = effect_levelup.displayObject.cachedTransform:Find("GoWrapper/effect_zrkjsj_SkeletonData")
            if effect_levelup_ani_obj ~= nil then
               this.tb_btn_attributes[index1].effect_levelup_ani  = effect_levelup_ani_obj:GetComponent("SkeletonAnimation")
               -- Util.PlayAction(this.tb_btn_attributes[index1].effect_levelup_ani, "", false)
            end
            this.tb_btn_attributes[index1].txt_skill_level = btn_attri:GetChild("text_level")
            this.tb_btn_attributes[index1].ctrl_skill_hero_type = btn_attri:GetController("HeroType")
            this.tb_btn_attributes[index1].ctrl_skill_max = btn_attri:GetController("Max")
            this.tb_btn_attributes[index1].icon = btn_attri:GetChild("n3"):GetChild("Icon")
            -- this.tb_btn_attributes[index1].icon.url = "ui://SkillAtlas/" .. "100005001"
            this.tb_btn_attributes[index1].btn_attribute = btn_attri
            this.tb_btn_attributes[index1].btn_attribute.onClick:Clear() 
            this.tb_btn_attributes[index1].btn_attribute.onClick:Add(function()
                 this.clickAttributeBtn( index1 ) 
            end)
        end

        this.tb_btn_tabs = {}
        this.cur_select_tab_index = 1
        for j=1, 6 do
            local index2 = j
            this.tb_btn_tabs[index2] = obj:GetChild("btn_tab0"..index2)
            this.tb_btn_tabs[index2].onClick:Clear()
            this.tb_btn_tabs[index2].onClick:Add(function()
                 this.clickTabBtn( index2 ) 
            end)
        end
        this.addEvent()

        this.cfg_guildskill_map = {}
        this.cfg_guildskill = G.dataTable["t_guildskill"]
        this.cfg_attributelibrary = G.dataTable["t_attributelibrary"] or require("Data/t_attributelibrary")

        for _, v in pairs(this.cfg_guildskill) do
            if v ~= nil then
                if this.cfg_guildskill_map[v.f_ProfessionalType] == nil then
                    this.cfg_guildskill_map[v.f_ProfessionalType] = {}
                end
                if this.cfg_guildskill_map[v.f_ProfessionalType][v.f_SkillClass] == nil then
                    this.cfg_guildskill_map[v.f_ProfessionalType][v.f_SkillClass] = {}
                end
                this.cfg_guildskill_map[v.f_ProfessionalType][v.f_SkillClass][v.f_SkillLevel] = v
            end
        end
        SetPlayerPrefs("ui_guild_tech", "1")
        FireEvent(Config.EventType.Fresh_Guild_Tech)
    end  
    -- this.refreshData()
    unionHandler.sendReqReqUnionTechInfo()
end

function UIGuildTech.UpRender(idx,obj)
    local skill = obj:GetChild("btn_skill")
    local skill_level = this.btn_skill:GetChild("text_level")
    local skill_hero_type = this.btn_skill:GetController("HeroType")
    skill_hero_type:SetSelectedIndex(0)
    local skill_max = this.btn_skill:GetController("Max")
    local group_name = obj:GetChild("text_name")
    local improve01 = obj:GetChild("com_improve01")
    local improve01_type = improve01:GetController("type")
    local improve01_herotype = improve01:GetChild("text_herotype")
    local improve01_title = improve01:GetChild("text_title")
    local improve01_numafter = improve01:GetChild("text_numAfter")
    local improve01_numbefore = improve01:GetChild("text_numBefore")
    local improve02 = obj:GetChild("com_improve02")
    local improve02_type = improve02:GetController("type")
    local improve02_herotype = improve02:GetChild("text_herotype")
    local improve02_title = improve02:GetChild("text_title")
    local improve02_numafter = improve02:GetChild("text_numAfter")
    local improve02_numbefore = improve02:GetChild("text_numBefore")

    local type_skill_data = this.cfg_guildskill_map[this.cur_select_tab_index]
    local cur_type_skill_data = this.cur_list_data[this.cur_select_tab_index]
    local skill_level_list = type_skill_data[this.cur_select_skill_index]
    local cur_skill_level = this.beforeUpLv
    local cur_level_item = skill_level_list[cur_skill_level]
    local next_level_item =skill_level_list[cur_type_skill_data[this.cur_select_skill_index].lv]
    local max_level_item = skill_level_list[#skill_level_list]
    local levelup_cost = cur_level_item["f_Cost"]
    skill_level.text = string.format("%s/%s", cur_level_item.f_SkillLevel,
            max_level_item and max_level_item.f_SkillLevel or cur_level_item.f_SkillLevel)
    improve01_herotype.text = getLanguage("HeroType" .. this.cur_select_tab_index)
    local cur_attr_info = cur_level_item.f_Attribute
    improve01_title.text = getLanguage("Attr_"..tostring(cur_attr_info[1]))
    group_name.text = getLanguage("Attr_"..tostring(cur_attr_info[1]))
    if next_level_item ~= nil then
        improve01_type:SetSelectedIndex(0)
        local cur_attr_value = this.GetAttributeValue( skill_level_list, cur_level_item )
        local next_attr_value = this.GetAttributeValue( skill_level_list, next_level_item )
        if this.cfg_attributelibrary[tostring(cur_attr_info[1])].f_AttributeType==1 then
            improve01_numbefore.text = "+" .. cur_attr_value -- tostring(cur_attr_info[2])
            improve01_numafter.text = "+" .. next_attr_value -- tostring(next_attr_info[2])
        else
            improve01_numbefore.text = "+" .. cur_attr_value/100 .."%" -- tostring(cur_attr_info[2])
            improve01_numafter.text = "+" .. next_attr_value/100 .."%"-- tostring(next_attr_info[2])
        end
    else
        improve01_type:SetSelectedIndex(3)
        improve01_numbefore.text = ""
        local cur_attr_value = this.GetAttributeValue( skill_level_list, cur_level_item )
        if this.cfg_attributelibrary[tostring(cur_attr_info[1])].f_AttributeType==1 then
            improve01_numafter.text = "+" .. cur_attr_value
        else
            improve01_numafter.text = "+" .. cur_attr_value/100 .."%"
        end

    end
    improve02_herotype.text = getLanguage("HeroType" .. this.cur_select_tab_index)

    if max_level_item ~= nil then
        local max_attri_info = max_level_item.f_AditionalAttribute
        improve02_title.text = getLanguage("Attr_"..tostring(max_attri_info[1]))
        improve02_numbefore.text = ""
        if this.cfg_attributelibrary[tostring(max_attri_info[1])].f_AttributeType==1 then
            improve02_numafter.text = "+"..tostring(max_attri_info[2])
        else
            improve02_numafter.text = "+"..tostring(max_attri_info[2]/100) .."%"
        end

        if next_level_item == nil then
            improve02_type:SetSelectedIndex(2)
        else
            improve02_type:SetSelectedIndex(1)
        end
    end
    local show_levelup_cost = nil
    if levelup_cost == nil or next(levelup_cost) == nil or next(levelup_cost[1]) == nil then
        skill_max:SetSelectedIndex(1)
        --this.ctrl_lock:SetSelectedIndex(1)
        local zero_level_item = skill_level_list[0]
        show_levelup_cost = zero_level_item["f_Cost"]
    else
        skill_max:SetSelectedIndex(0)
        --this.ctrl_lock:SetSelectedIndex(0)
        show_levelup_cost =  levelup_cost
    end
    skill:GetChild("n3"):GetChild("Icon").url = "ui://SkillAtlas/" .. cur_level_item.f_SkillIcon
end

function UIGuildTech.GetAttributeValue( skill_level_list, item ) 
    local value = 0
    for i = 0, item.f_SkillLevel do
        if skill_level_list[i] ~= nil then
            value = value + skill_level_list[i].f_Attribute[2]
        end
    end
    return value
end
--- 根据职业更新总等级
function UIGuildTech.updateDataByType(index)
    local curNum, maxNum;
    local _begin = index or 1;
    local _end = index or 6;
    for j = _begin, _end do -- 6个职业
        curNum = 0;
        maxNum = 0;
        local type_skill_data = this.cfg_guildskill_map[j]
        local cur_type_skill_data = this.cur_list_data[j]
        for i = 1, 6 do -- 6个科技
            local skill_level_list = type_skill_data[i]
            local cur_skill_level = cur_type_skill_data[i].lv
            local cur_level_item = skill_level_list[cur_skill_level]
            local max_level_item = skill_level_list[#skill_level_list]
            curNum = curNum + cur_level_item.f_SkillLevel;
            maxNum = maxNum + max_level_item.f_SkillLevel;
        end
        if this.tb_btn_tabs[j] then
            this.tb_btn_tabs[j]:GetChild("text_level").text = string.format("%s/%s", curNum, maxNum);
        end
    end
end

function UIGuildTech.refreshData()

    local type_skill_data = this.cfg_guildskill_map[this.cur_select_tab_index]
    local cur_type_skill_data = this.cur_list_data[this.cur_select_tab_index]
    for i=1, 6 do
        local skill_level_list = type_skill_data[i] 
        local cur_skill_level = cur_type_skill_data[i].lv
        local cur_level_item = skill_level_list[cur_skill_level] --cur_type_skill_data[i]
        local max_level_item = nil 
        local next_level_item =skill_level_list[cur_skill_level + 1]
        local max_level_item = skill_level_list[#skill_level_list]

        local levelup_cost = cur_level_item["f_Cost"]
        local com_tb = this.tb_btn_attributes[i]
        if com_tb ~= nil then
            com_tb.txt_skill_level.text = string.format("%s/%s", cur_level_item.f_SkillLevel, 
                max_level_item and max_level_item.f_SkillLevel)
            if this.cur_select_skill_index == i then
                com_tb.ctrl_skill_hero_type:SetSelectedIndex(this.cur_select_tab_index) 
            else
                com_tb.ctrl_skill_hero_type:SetSelectedIndex(0) 
            end
            if next_level_item == nil then
                com_tb.ctrl_skill_max:SetSelectedIndex(1)
            else
                com_tb.ctrl_skill_max:SetSelectedIndex(0)
            end 
            com_tb.icon.url = "ui://SkillAtlas/" .. cur_level_item.f_SkillIcon
        end

        if this.cur_select_skill_index == i then
            this.txt_skill_level.text = string.format("%s/%s", cur_level_item.f_SkillLevel,
                max_level_item and max_level_item.f_SkillLevel or cur_level_item.f_SkillLevel)
            -- this.ctrl_skill_hero_type:SetSelectedIndex(this.cur_select_tab_index) 
            this.improve01_txt_herotype.text = getLanguage("HeroType" .. this.cur_select_tab_index)
            local cur_attr_info = cur_level_item.f_Attribute
            this.improve01_txt_title.text = getLanguage("Attr_"..tostring(cur_attr_info[1]))
            this.group_txt_name.text = getLanguage("Attr_"..tostring(cur_attr_info[1]))
            if next_level_item ~= nil then
                this.ctrl_improve01_type:SetSelectedIndex(0) 
                local next_attr_info = next_level_item.f_Attribute
                local cur_attr_value = this.GetAttributeValue( skill_level_list, cur_level_item ) 
                local next_attr_value = this.GetAttributeValue( skill_level_list, next_level_item )
                if this.cfg_attributelibrary[tostring(cur_attr_info[1])].f_AttributeType==1 then
                    this.improve01_txt_numbefore.text = "+" .. cur_attr_value -- tostring(cur_attr_info[2])
                    this.improve01_txt_numafter.text = "+" .. next_attr_value -- tostring(next_attr_info[2])
                else
                    this.improve01_txt_numbefore.text = "+" .. cur_attr_value/100 .."%" -- tostring(cur_attr_info[2])
                    this.improve01_txt_numafter.text = "+" .. next_attr_value/100 .."%"-- tostring(next_attr_info[2])
                end

            else
                this.ctrl_improve01_type:SetSelectedIndex(3) 
                this.improve01_txt_numbefore.text = ""
                local cur_attr_value = this.GetAttributeValue( skill_level_list, cur_level_item )
                if this.cfg_attributelibrary[tostring(cur_attr_info[1])].f_AttributeType==1 then
                    this.improve01_txt_numafter.text = "+" .. cur_attr_value
                else
                    this.improve01_txt_numafter.text = "+" .. cur_attr_value/100 .."%" -- tostring(next_attr_info[2])
                end
               --tostring(cur_attr_info[2])
            end
            this.improve02_txt_herotype.text = getLanguage("HeroType" .. this.cur_select_tab_index)
            
            if max_level_item ~= nil then 
                local max_attri_info = max_level_item.f_AditionalAttribute 
                this.improve02_txt_title.text = getLanguage("Attr_"..tostring(max_attri_info[1]))
                this.improve02_txt_numbefore.text = ""

                if this.cfg_attributelibrary[tostring(max_attri_info[1])].f_AttributeType==1 then
                    this.improve02_txt_numafter.text = "+"..tostring(max_attri_info[2])
                else
                    this.improve02_txt_numafter.text = "+"..tostring(max_attri_info[2]/100) .. "%"
                end
                if next_level_item == nil then
                    this.ctrl_improve02_type:SetSelectedIndex(2)
                else
                    this.ctrl_improve02_type:SetSelectedIndex(1)
                end
            end

            local show_levelup_cost = nil
            if levelup_cost == nil or next(levelup_cost) == nil or next(levelup_cost[1]) == nil then
                this.ctrl_skill_max:SetSelectedIndex(1)
                this.ctrl_lock:SetSelectedIndex(1)
                local zero_level_item = skill_level_list[0]
                show_levelup_cost = zero_level_item["f_Cost"]
            else
                this.ctrl_skill_max:SetSelectedIndex(0)
                this.ctrl_lock:SetSelectedIndex(0) 
                show_levelup_cost =  levelup_cost
            end 
            this.btn_skill:GetChild("n3"):GetChild("Icon").url = "ui://SkillAtlas/" .. cur_level_item.f_SkillIcon

            if show_levelup_cost ~= nil and next(show_levelup_cost) ~= nil then
                local cost_item1_url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(show_levelup_cost[1][1])
                this.img_coin01.url = cost_item1_url
                this.img_coin03.url = cost_item1_url
                local own_num1 = logicMgr.ItemManager.getBagNumByTableID(show_levelup_cost[1][1])
                this.txt_coin01.text = HelperFunc.FormatResourceNum(own_num1, false)
                local cost_num1 = tonumber( tostring(show_levelup_cost[1][2]) )
                this.txt_coin03.text =  HelperFunc.FormatResourceNum( cost_num1, false)

                local cost_item2_url = EnumConst.AtlasPath.ItemAtlas..logicMgr.ItemManager.GetItemIcon(show_levelup_cost[2][1])
                this.img_coin02.url = cost_item2_url
                this.img_coin04.url = cost_item2_url
                local own_num2 = logicMgr.ItemManager.getBagNumByTableID(show_levelup_cost[2][1])
                local cost_num2 = tonumber(tostring(show_levelup_cost[2][2]))
                this.txt_coin02.text = HelperFunc.FormatResourceNum( own_num2, false)
                this.txt_coin04.text = HelperFunc.FormatResourceNum( cost_num2 , false)

                if own_num1 >= cost_num1 and own_num2 >= cost_num2 then
                    this.ctrl_item_enough:SetSelectedIndex(0) 
                elseif own_num1 >= cost_num1 and own_num2 < cost_num2 then
                    this.ctrl_item_enough:SetSelectedIndex(1) 
                elseif own_num1 < cost_num1 and own_num2 >= cost_num2 then
                    this.ctrl_item_enough:SetSelectedIndex(2) 
                else
                    this.ctrl_item_enough:SetSelectedIndex(3) 
                end

            end
        end
    end
end

function UIGuildTech.clickAttributeBtn( index )
    if this.cur_select_skill_index ~= index then
        this.cur_select_skill_index = index
        this.refreshData()
    end
end

function UIGuildTech.clickTabBtn( index ) 
    this.cur_select_tab_index = index
    this.refreshData()
end

function UIGuildTech.addEvent()
    this.btn_close.onClick:Add(this.onClickClose) 
    this.btn_help.onClick:Add(this.onClickHelp) 
    this.btn_reset.onClick:Add(this.onClickReset) 
    this.btn_levelup.onClick:Add(this.onClickLevelUp) 
    this.btn_quicklevelup.onClick:Add(this.onClickQuickLevelUp) 
    -- this.btn_skill.onClick:Add(this.onClickSkill)
    this.btn_source.onClick:Add(this.onClickSource)
    this.bgClose.onClick:Add(function()
        this.lvUpCtrl.selectedIndex = 0
    end)
end

function UIGuildTech.removeEvent()
    this.btn_close.onClick:Clear() 
    this.btn_help.onClick:Clear() 
    this.btn_reset.onClick:Clear() 
    this.btn_levelup.onClick:Clear() 
    this.btn_quicklevelup.onClick:Clear() 
    -- this.btn_skill.onClick:Clear() 
    this.btn_source.onClick:Clear()
    this.bgClose.onClick:Clear()
end

function UIGuildTech.onClickSource()
    this.ctrl_show_source.selectedIndex = 1
    this.showSourceList = {CurrencyEnum.GuildTechMoeny}
    this.updateSource()
end
 
function UIGuildTech.updateSource()
    logicMgr.ItemManager.updateSourceUI(this.sourceUIobj, this.showSourceList, function ()
        this.ctrl_show_source.selectedIndex = 0
    end, function ()
        this.ctrl_show_source.selectedIndex = 0
        closeUI(PanelNames.UIGuildTech)
    end );
end

function UIGuildTech.onClickClose()
    closeUI(PanelNames.UIGuildTech)
    logicMgr.HeroManager.UpdateAllAttr()
    partnerHandler.ReqHeroPower(0)
    FireEvent(Config.EventType.ReFreshHeroPower)
end

function UIGuildTech.onClickHelp() 
    local txts = {} 
    txts["title"] = "Draw_Detail_Des"
    txts["des"] = "GuildTechRules"
    showUI(PanelNames.CommonTips, txts)
end

function UIGuildTech.onClickReset()  
    
    local type_skill_data = this.cfg_guildskill_map[this.cur_select_tab_index]
    local cur_type_skill_data = this.cur_list_data[this.cur_select_tab_index]
    local cost_items = {}
    for i=1, 6 do
        local skill_level_list = type_skill_data[i] 
        local cur_skill_level = cur_type_skill_data[i].lv-1
        local cur_level_item = skill_level_list[cur_skill_level]
        for j=0, cur_skill_level do
            local item =  skill_level_list[j]
            local tb_cost_item = decodeJsonStrToTabel(item["f_Cost"])
            if tb_cost_item ~= nil and next(tb_cost_item) ~= nil and  tb_cost_item[1] ~= nil and next(tb_cost_item[1]) ~= nil then
                for _, v in pairs(tb_cost_item) do 
                    local item_id =  v[1]
                    if cost_items[item_id] == nil then
                        cost_items[item_id] = 0
                    end
                    cost_items[item_id] = cost_items[item_id] + tonumber(v[2]) 
                end
            end
        end
    end
    if next(cost_items) == nil then
        UISysTips.AddMsg(getLanguage("GuildTechtips"))
        return
    end

    local rewards = {} 
    for k, v in pairs(cost_items) do
        local item = {}
        item.itemModelId = k
        item.num = v
        table.insert(rewards, item)
    end
    showUI(PanelNames.UIResetPop, rewards, function()
            local cost_num = dataTable.getDataTableByStringId("t_global", 501 ,"f_int_value")
            if cost_num > logicMgr.ItemManager.getBagNumByTableID( 1 ) then
                UISysTips.AddMsg(getLanguage("ErrorCode_1311"))
            else 
                local type_skill_data = this.cfg_guildskill_map and this.cfg_guildskill_map[this.cur_select_tab_index]
                if type_skill_data ~= nil then
                    local item = type_skill_data[1][1]
                    unionHandler.sendReqUnionResetTech( item["f_GuildLayer"], this.cur_select_tab_index)
                end
            end
    end )
end

function UIGuildTech.onClickLevelUp() 
    local tab_data = this.cur_list_data and this.cur_list_data[this.cur_select_tab_index]

    local type_skill_data = this.cfg_guildskill_map[this.cur_select_tab_index]
    if tab_data ~= nil then
        local cur_select_skill_level = tab_data[this.cur_select_skill_index].lv
        local next_type_skill_data = type_skill_data[this.cur_select_skill_index][tab_data[this.cur_select_skill_index].lv+1]
        if next_type_skill_data == nil then
            UISysTips.AddMsg(getLanguage("ErrorCode_316"))
            return
        end

        local cur_type_skill_data = type_skill_data[this.cur_select_skill_index][tab_data[this.cur_select_skill_index].lv]
        local tb_cost_item = decodeJsonStrToTabel(cur_type_skill_data["f_Cost"])
        local enough = true
        -- local owner_num = 0
        for _, v in pairs(tb_cost_item) do
            if logicMgr.ItemManager.getBagNumByTableID(v[1]) < tonumber(v[2]) then
                enough = false
                break
            end
        end
        if enough then
            this.upgrade_type_str = "union_tech_upgrade"
            this.cur_guild_tech_skill_id = tab_data[this.cur_select_skill_index].techId
            this.cur_profession_type = this.cur_select_tab_index
            this.cur_skill_level = cur_select_skill_level
            this.cur_skill_change_level_num = 1
            this.beforeUpLv = this.cur_list_data[this.cur_select_tab_index][this.cur_select_skill_index].lv
            unionHandler.sendReqUnionUpgradeTech( cur_type_skill_data["f_GuildSkillId"], 1)
        else
            UISysTips.AddMsg(getLanguage("Character_Tips2")) 
        end
    end
end

function UIGuildTech.onClickQuickLevelUp()
    local tab_data = this.cur_list_data and this.cur_list_data[this.cur_select_tab_index]
    if tab_data ~= nil then 
        local cur_type_skill_data = tab_data[this.cur_select_skill_index]
        local skill_level_list = this.cfg_guildskill_map[this.cur_select_tab_index][this.cur_select_skill_index]
        local next_level_skill_data = skill_level_list[cur_type_skill_data.lv + 1]
        if next_level_skill_data == nil then
            UISysTips.AddMsg(getLanguage("ErrorCode_316"))
            return
        end
        local upgrade_num = 0
        local cur_skill_level = cur_type_skill_data.lv --cur_type_skill_data["f_SkillLevel"]
        local cur_cost_num = {}
        for i=cur_skill_level, #skill_level_list do
            local tb_cost_item = decodeJsonStrToTabel(skill_level_list[i]["f_Cost"])
            if tb_cost_item ~= nil and next(tb_cost_item) ~= nil and  tb_cost_item[1] ~= nil and next(tb_cost_item[1]) ~= nil then
                local enough = true
                for k, v in pairs(tb_cost_item) do
                    local item_id =  v[1]
                    if cur_cost_num[item_id] == nil then
                        cur_cost_num[item_id] = 0
                    end
                    cur_cost_num[item_id] = cur_cost_num[item_id] + tonumber(v[2])
                    if logicMgr.ItemManager.getBagNumByTableID(item_id) < cur_cost_num[item_id] then
                         enough = false
                         break
                    end
                end
                if enough then
                    upgrade_num = upgrade_num + 1
                else
                    break
                end
            end
        end
        if upgrade_num > 0 then
            this.upgrade_type_str = "union_tech_onekey_upgrade"
                this.cur_guild_tech_skill_id = cur_type_skill_data.techId
                this.cur_profession_type = this.cur_select_tab_index
                this.cur_skill_level = cur_type_skill_data.lv
                this.cur_skill_change_level_num = upgrade_num
            this.beforeUpLv = this.cur_list_data[this.cur_select_tab_index][this.cur_select_skill_index].lv
            unionHandler.sendReqUnionUpgradeTech( cur_type_skill_data.techId,  upgrade_num) --cur_type_skill_data["f_GuildSkillId"], upgrade_num)
        else
            UISysTips.AddMsg(getLanguage("Character_Tips2"))
        end
    end
end

-- function UIGuildTech.onClickSkill() 
--     print("UIGuildTech.onClickSkill--->")
-- end

function UIGuildTech.LocalListenEvent()
    -- ListenEvent(Config.EventType.ItemResolve,this.ReceiveItemResolveMsg)
end

function UIGuildTech.LocalCloseEvent()
    -- CloseEvent(Config.EventType.ItemResolve,this.ReceiveItemResolveMsg)
end

---- 应答科技列表数据
function UIGuildTech.UpdateUnionTechInfoListData(msg) 
    if msg and msg.techs then
        this.cur_list_data = {}
        local key = nil
        local value = nil
        for _, v in pairs(msg.techs) do
            key = v.techId + v.lv 
            value = this.cfg_guildskill[tostring(key)]
            if this.cur_list_data[value.f_ProfessionalType] == nil then
                this.cur_list_data[value.f_ProfessionalType] = {}
            end
            this.cur_list_data[value.f_ProfessionalType][value.f_SkillClass] = v
        end
        
        this.refreshData()
        this.updateDataByType()
    end
end

---- 应答科技列表数据
function UIGuildTech.UpdateUnionUpgradeTechResult(msg)
    if msg and msg.tech then
        local tech = msg.tech
        local key = tech.techId + tech.lv 
        local value = this.cfg_guildskill[tostring(key)]
        if this.cur_list_data[value.f_ProfessionalType] == nil then
            this.cur_list_data[value.f_ProfessionalType] = {}
        end
        this.cur_list_data[value.f_ProfessionalType][value.f_SkillClass] = tech

        local effect_levelup_ani = this.tb_btn_attributes[this.cur_select_skill_index].effect_levelup_ani
        if effect_levelup_ani ~= nil then
            Util.PlayAction(effect_levelup_ani, "idle", false)
        end
        this.upList.numItems = 1
        this.lvUpCtrl.selectedIndex = 1
        this.lvUptitle.text = getLanguage("GuildInvestment_1152")
        this.refreshData()
        this.updateDataByType(value.f_ProfessionalType)
        this.UnionTechUpgradeCollectData()

        partnerHandler.ReqHeroPower(0)
    end
end

-- 升级公会技能埋点
function UIGuildTech.UnionTechUpgradeCollectData()
    if this.upgrade_type_str ~= nil then
        local data           = {}
        data.guild_tech_skill_id = tostring(this.cur_guild_tech_skill_id)
        data.profession_type  = tostring(this.cur_profession_type)
        data.cur_skill_level  = tostring(this.cur_skill_level)
        data.cur_skill_upgrade_num  = tostring(this.cur_skill_change_level_num)
        TAManager.TrackByDictionary(this.upgrade_type_str, data)
    end
    this.upgrade_type_str = nil
    this.cur_guild_tech_skill_id = nil
    this.cur_profession_type = nil
    this.cur_skill_level = nil
    this.cur_skill_change_level_num = nil
end

---- 返回重置结果
function UIGuildTech.UpdateUnionResetTechResult(msg)
    if msg and msg.techs then
        local key = nil
        local value = nil
        for _, v in pairs(msg.techs) do
            key = v.techId + v.lv 
            value = this.cfg_guildskill[tostring(key)]
            if this.cur_list_data[value.f_ProfessionalType] == nil then
                this.cur_list_data[value.f_ProfessionalType] = {}
            end
            this.cur_list_data[value.f_ProfessionalType][value.f_SkillClass] = v
        end
        this.refreshData()
        this.updateDataByType()
        -- if msg.items ~= nil and next(msg.items) ~= nil then
        --     local rewards = {}
        --     for _, v in pairs(msg.items) do
        --         local item = {}
        --         item.itemModelId = v.itemModelId
        --         item.num = tonumber(tostring(v.num))
        --         table.insert(rewards, item)
        --     end
        --     if #rewards > 0 then
        --         showUI(PanelNames.UICommonItemGet, nil, nil, 3, rewards);
        --     end
        -- end

        for i=1, 6 do
            local effect_levelup_ani = this.tb_btn_attributes[i] and this.tb_btn_attributes[i].effect_levelup_ani
            if effect_levelup_ani ~= nil then
                Util.PlayAction(effect_levelup_ani, "idle", false)
            end
        end

        partnerHandler.ReqHeroPower(0)
    end
end

function UIGuildTech.OnDestroy()
    this.removeEvent()
    if this.tb_btn_attributes ~= nil then
        for _, v in pairs(this.tb_btn_attributes) do
            v.btn_attribute.onClick:Clear() 
            v.btn_attribute = nil
            v.txt_skill_level = nil
            v.ctrl_skill_hero_type = nil
            v.ctrl_skill_max = nil
            v.icon = nil
            v.effect_levelup_ani = nil
            v = nil
        end
        this.tb_btn_attributes = nil
    end
    if this.tb_btn_tabs ~= nil then
        for _, v in pairs(this.tb_btn_tabs) do
             v.onClick:Clear() 
             v = nil
        end
        this.tb_btn_tabs = nil
    end

    this.cfg_guildskill_map = nil
    this.cfg_guildskill = nil
end

function UIGuildTech.OnEnable()
    this.LocalListenEvent()
end

function UIGuildTech.OnDisable()
    this.LocalCloseEvent()
end
 
return UIGuildTech
